package kr.co.graviti.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.common.application.MultipartUploadFileService;
import kr.co.graviti.common.model.MultipartUploadFile;
import kr.co.graviti.common.util.CommonVariable;
import kr.co.graviti.service.daumeditor.DaumeditorService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DaumUploadController {
	
	@Value("${common.default.upload.path}") private String uploadDefaultPath;
	@Value("${common.daumeditor.upload.path}") private String daumeditorUpLoadPath;
	@Value("${common.upload.allow_postfix}") private String uploadAllowPostfix;
	@Value("${common.upload.size}") private long uploadSize;
	
	@Value("${common.default.download.path}") private String defultpath;
	@Value("${common.domain}") private String sitedomain;
	@Value("${common.daumeditor.down.path}") private String daumeditorDownLoadPath;
	
	@Resource
	private MultipartUploadFileService multipartUploadFileService;
	
	@Resource
	private DaumeditorService daumeditorservice;
	
	public int icnt = 0;
	
	@RequestMapping(value="/include/fileinfo")
	public ModelAndView fileinfo(HttpServletRequest request 
			,HttpServletResponse response
			,@RequestParam(value="imgfname", required=false, defaultValue="") String [] imgfname){
		
		
		HashMap<String, Object> param	= new HashMap<String, Object>();
		param.put("tmptx_old", request.getRemoteAddr() + CommonVariable.DAUMEDITOR_PREPARATION);
		param.put("tmptx_new", request.getRemoteAddr() + CommonVariable.DAUMEDITOR_COMPLETE);		
		List<HashMap<String, Object>> list = daumeditorservice.select_editorFile(param);
		
		// 기존 데이터 업로드 완성 확인.
		daumeditorservice.update_editorFile(param);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("fileAllPath",sitedomain + defultpath + daumeditorDownLoadPath + "/");
		mav.addObject("list",list);
		mav.setViewName(new String("include/fileinfo"));
		return mav;
	}

	@RequestMapping(value="/include/fileUpload")
	public ModelAndView fileUpload(HttpServletRequest request 
			,HttpServletResponse response
			,@RequestParam(value="file", required=false,defaultValue="") MultipartFile upfiles
			){
		
		HashMap<String, Object> param	= new HashMap<String, Object>();
		param.put("userid", request.getSession().getAttribute(CommonVariable.SESSION_ADMIN_ID));
		param.put("tmptx", request.getRemoteAddr() + CommonVariable.DAUMEDITOR_PREPARATION);
		
		MultipartUploadFile multipartUploadFile = new MultipartUploadFile();
		icnt = icnt + 1;
		multipartUploadFile = multipartUploadFileService.regist("daumeditor_"+Integer.toString(icnt)
				, uploadDefaultPath+daumeditorUpLoadPath
				, uploadSize
				, uploadAllowPostfix
				, upfiles);
		
		param.put("org_filenm", multipartUploadFile.getOrginalFilename());
		param.put("chg_filenm", multipartUploadFile.getFilename());
		param.put("file_size", multipartUploadFile.getFilesize());
		// DB에 저장 
		daumeditorservice.insert_editorFile(param);
		ModelAndView mav = new ModelAndView();
		return mav;
	}	
}
